package com.hhl.flowlayoutdemo.slice;

import com.github.mrengineer13.snackbar.SnackBar;
import com.hhl.flowlayoutdemo.ResourceTable;
import com.hhl.library.FlowTagLayout;
import com.hhl.library.LogUtil;
import com.hhl.library.OnTagClickListener;
import com.hhl.library.OnTagSelectListener;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {

  private FlowTagLayout mColorFlowTagLayout;
  private FlowTagLayout mSizeFlowTagLayout;
  private FlowTagLayout mMobileFlowTagLayout;
  private TagAdapter<String> mSizeTagAdapter;
  private TagAdapter<String> mColorTagAdapter;
  private TagAdapter<String> mMobileTagAdapter;
  private Component selectIndex = null;
  private List<Component> selectComponent;
  private short duration = 500;
  private String message;

  @Override
  public void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_main);


    mColorFlowTagLayout = (FlowTagLayout) findComponentById(ResourceTable.Id_color_flow_layout);
    mSizeFlowTagLayout = (FlowTagLayout) findComponentById(ResourceTable.Id_size_flow_layout);
    mMobileFlowTagLayout = (FlowTagLayout) findComponentById(ResourceTable.Id_mobile_flow_layout);

    //颜色
    mColorTagAdapter = new TagAdapter<>(this);
    mColorFlowTagLayout.setAdapter(mColorTagAdapter);
    Component component = findComponentById(ResourceTable.Id_main);
    mColorFlowTagLayout.setOnTagClickListener(new OnTagClickListener() {
      @Override
      public void onItemClick(FlowTagLayout parent, Component view, int position) {
        LogUtil.debug(" 点击： ", "" + parent.getAdapter().getItem(position));

        message = "颜色:" + parent.getAdapter().getItem(position);
        new SnackBar.Builder(getAbility(), component)
            .withMessage(message)
            .withBackgroundColorId(Color.GRAY.getValue())
            .withDuration(duration)
            .withTypeFace(Font.DEFAULT)
            .show();
      }
    });

    //尺寸
    mSizeTagAdapter = new TagAdapter<>(this);
    mSizeFlowTagLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
    mSizeFlowTagLayout.setAdapter(mSizeTagAdapter);
    mSizeFlowTagLayout.setOnTagSelectListener(new OnTagSelectListener() {
      @Override
      public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
        if (selectIndex != null) {
          selectIndex.setBackground(new ShapeElement(getContext(),
              ResourceTable.Graphic_bg_label_unchecked));
        }
        if (selectedList != null && selectedList.size() > 0) {
          StringBuilder sb = new StringBuilder();
          for (int i : selectedList) {
            sb.append(parent.getAdapter().getItem(i));
            sb.append(":");
            selectIndex = parent.getComponentAt(i);
            selectIndex.setBackground(new ShapeElement(getContext(),
                ResourceTable.Graphic_bg_label_checked));
          }
          message = sb.toString();
          LogUtil.debug(" 单选： ", sb.toString());
        } else {
          message = "没有选择标签";
        }
        new SnackBar.Builder(getAbility(), component)
            .withMessage(message)
            .withBackgroundColorId(Color.GRAY.getValue())
            .withDuration(duration)
            .withTypeFace(Font.DEFAULT)
            .show();
      }
    });

    //移动研发标签
    mMobileTagAdapter = new TagAdapter<>(this);
    mMobileFlowTagLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_MULTI);
    mMobileFlowTagLayout.setAdapter(mMobileTagAdapter);
    mMobileFlowTagLayout.setOnTagSelectListener(new OnTagSelectListener() {
      @Override
      public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
        if (selectedList != null && selectedList.size() > 0) {
          selectComponent = new ArrayList<>();
          StringBuilder sb = new StringBuilder();
          for (int i : selectedList) {
            sb.append(parent.getAdapter().getItem(i));
            sb.append(":");
            selectComponent.add(parent.getComponentAt(i));
          }
          for (int i = 0; i < parent.getAdapter().getCount(); i++) {
            for (int j = 0; j < selectComponent.size(); j++) {
              if (selectComponent.contains(parent.getComponentAt(i))) {
                parent.getComponentAt(i).setBackground(new ShapeElement(getContext(),
                    ResourceTable.Graphic_bg_label_checked));
              } else {
                parent.getComponentAt(i).setBackground(new ShapeElement(getContext(),
                    ResourceTable.Graphic_bg_label_unchecked));
              }
            }
          }
          LogUtil.debug(" 多选： ", sb.toString());
          message = sb.toString();
        } else {
          if (selectComponent != null && selectComponent.size() > 0) {
            selectComponent.get(0).setBackground(new ShapeElement(getContext(),
                ResourceTable.Graphic_bg_label_unchecked));
            selectComponent = new ArrayList<>();
          }
          message = "没有选择标签";
        }
        new SnackBar.Builder(getAbility(), component)
            .withMessage(message)
            .withBackgroundColorId(Color.GRAY.getValue())
            .withDuration(duration)
            .withTypeFace(Font.DEFAULT)
            .show();
      }
    });

    initColorData();

    initSizeData();

    initMobileData();

    //清除所有已经被选择的选项
    findComponentById(ResourceTable.Id_bt_clear_all).setClickedListener(new Component.ClickedListener() {
      @Override
      public void onClick(Component component) {
        mSizeFlowTagLayout.clearAllOption();
        mMobileFlowTagLayout.clearAllOption();
        if (selectIndex != null) {
          selectIndex.setBackground(new ShapeElement(getContext(), ResourceTable.Graphic_bg_label_unchecked));
          selectIndex = null;
        }
        if (selectComponent != null) {
          for (int j = 0; j < selectComponent.size(); j++) {
            selectComponent.get(j).setBackground(new ShapeElement(getContext(),
                ResourceTable.Graphic_bg_label_unchecked));
          }
          selectComponent = new ArrayList<>();
        }
      }
    });
  }

  private void initMobileData() {
    List<String> dataSource = new ArrayList<>();
    dataSource.add("移动");
    dataSource.add("ohos");
    dataSource.add("SDK源码");
    dataSource.add("IOS");
    dataSource.add("iPhone");
    dataSource.add("游戏");
    dataSource.add("fragment");
    dataSource.add("viewcontroller");
    dataSource.add("cocoachina");
    dataSource.add("移动研发工程师");
    dataSource.add("移动互联网");
    dataSource.add("高薪+期权");
    mMobileTagAdapter.onlyAddAll(dataSource);
  }

  private void initColorData() {
    List<String> dataSource = new ArrayList<>();
    dataSource.add("红色");
    dataSource.add("黑色");
    dataSource.add("花边色");
    dataSource.add("深蓝色");
    dataSource.add("白色");
    dataSource.add("玫瑰红色");
    dataSource.add("紫黑紫兰色");
    dataSource.add("葡萄红色");
    dataSource.add("屎黄色");
    dataSource.add("绿色");
    dataSource.add("彩虹色");
    dataSource.add("牡丹色");
    mColorTagAdapter.onlyAddAll(dataSource);
  }

  /**
   * 初始化数据.
   */
  private void initSizeData() {
    List<String> dataSource = new ArrayList<>();
    dataSource.add("28 (2.1尺)");
    dataSource.add("29 (2.2尺)");
    dataSource.add("30 (2.3尺)");
    dataSource.add("31 (2.4尺)");
    dataSource.add("32 (2.5尺)........");
    dataSource.add("33 (2.6尺)");
    dataSource.add("34 (2.7尺)");
    dataSource.add("35 (2.8尺)");
    dataSource.add("36 (2.9尺)");
    dataSource.add("37 (3.0尺)");
    dataSource.add("38 (3.1尺)");
    dataSource.add("39 (3.2尺)........");
    mSizeTagAdapter.onlyAddAll(dataSource);
  }

  @Override
  public void onActive() {
    super.onActive();
  }

  @Override
  public void onForeground(Intent intent) {
    super.onForeground(intent);
  }
}
